package cn.mengfly.testnrz.json;

import cn.mengfly.testnrz.crazyIm.nettyserver.ImNode;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.GsonBuilder;

import java.nio.charset.StandardCharsets;

public class JsonUtil {
    static GsonBuilder gb = new GsonBuilder();

    static {
        gb.disableHtmlEscaping();
    }

    public static String pojoToJson(Object obj) {
        return gb.create().toJson(obj);
    }

    public static <T> T jsonToPojo(String json, Class<T> cls) {
        return JSONObject.parseObject(json, cls);
    }

    public static byte[] object2JsonBytes(Object obj) {
        String json = pojoToJson(obj);
        return json.getBytes(StandardCharsets.UTF_8);
    }

    public static <T> T jsonBytes2Object(byte[] bytes, Class<T> cls) {
        String json = new String(bytes, StandardCharsets.UTF_8);
        return jsonToPojo(json, cls);
    }
}
